//监控点，污染，联系人的缓存变量，用来进行取消操作后的重新赋值
var monitoringPoint = null;
var contaminants = null;
var contacts = null;
var monitorPointController = MonitorPointController();
var Global_Data = null;


function EssentialService() {
    var service = {};
    //通过父页面传来的mPointId,查找出所有的相关信息，并且加载到表格中
    service.getMonitorPointBaseInfoById = function (monitorPointId) {
        monitorPointController.getMonitorPointBaseInfoById(monitorPointId,function (resp) {
            var data = resp.data;
            Global_Data = data;
            service.formatMonitoringPointTable(data);
            service.formatContaminantsTable(data);
            service.formatContactTable(data);
        });
    };
    service.updateMonitorPointBaseInfoById = function(baseInfo){
        monitorPointController.updateMonitorPointBaseInfoById(baseInfo,function (resp) {
           if(resp.result){
               layer.alert(resp.description);
           }else{
               $("#save").attr('hidden', true);
               $("#cancel").attr('hidden', true);
               $('#contaminants').html(contaminants);
               $('#monitoringPoint').html(monitoringPoint);
               $('#contacts').html(contacts);
               layer.alert(resp.description);
           }
        });
    };
    //监控点表格的处理(查询时的格式化)
    service.formatMonitoringPointTable = function (data) {
        $("#monitoringPointName").html(data.baseInfo.name);
        $("#monitoringPointPos").html(data.baseInfo.address);
        $("#monitoringPointLng").html(data.baseInfo.longitude);
        $("#monitoringPointLat").html(data.baseInfo.latitude);
        $("#monitoringPointStyle").html(data.baseInfo.markInstallationWay);
        if(data.baseInfo.enableStatus === 1){
            $("#monitoringPointState").html('启用');
        }else{
            $("#monitoringPointState").html('禁用');
        }
    };
    //污染物信息表格的处理(查询时的格式化)
    service.formatContaminantsTable = function (data) {
        data.thresholdConfigs.map(function (item) {
            var $trTemp = $("<tr></tr>");
            $trTemp.data('id',item.id);
            //往行里面追加 td单元格
            $trTemp.append("<td>"+ item.itemName +"</td>");
            $trTemp.append("<td>"+ item.normalValueLowerLimit +"</td>");
            $trTemp.append("<td>"+ item.normalValueUpperLimit +"</td>");
            $trTemp.append("<td>"+ item.alarmUpperLimit +"</td>");
            $trTemp.append("<td>"+ item.alarmLowerLimit +"</td>");
            if(item.status == 1){
                $trTemp.append("<td>"+ '启用' +"</td>");
            }else{
                $trTemp.append("<td>"+ '禁用' +"</td>");
            }
            $trTemp.appendTo("#contaminants");
        });
    };
    service.intervalJudgement = function(normalValueLowerLimit,normalValueUpperLimit,alarmUpperLimit,alarmLowerLimit){
        if(normalValueLowerLimit<=normalValueUpperLimit && alarmLowerLimit <=alarmUpperLimit){
            if(normalValueLowerLimit <= alarmLowerLimit && normalValueUpperLimit >= alarmUpperLimit){
                return true
            }else{
                return false;
            }
        }else{
            return false;
        }
    };
    //联系方式表格的处理(查询时的格式化)
    service.formatContactTable = function (data) {
        var length = data.alarmPushConfigs.length;
        if(length % 2 == 0){
            for(var i=0;i<length;i=i+2){
                var $trTemp = $("<tr></tr>");
                $trTemp.append("<td>"+ data.alarmPushConfigs[i].targetName +"</td>");
                $trTemp.append("<td>"+ data.alarmPushConfigs[i].targetPhoneNumber +"</td>");
                $trTemp.append("<td>"+ data.alarmPushConfigs[i+1].targetName +"</td>");
                $trTemp.append("<td>"+ data.alarmPushConfigs[i+1].targetPhoneNumber +"</td>");
                $trTemp.appendTo("#contacts");
            }

            for(var j=0;j<6-length;j=j+2){
                var $trTemp = $("<tr></tr>");
                $trTemp.append("<td>"+ "" +"</td>");
                $trTemp.append("<td>"+ "" +"</td>");
                $trTemp.append("<td>"+ '' +"</td>");
                $trTemp.append("<td>"+ '' +"</td>");
                $trTemp.appendTo("#contacts");
            }
        }else{
            for(var i=0;i<length-1;i=i+2){
                var $trTemp = $("<tr></tr>");
                $trTemp.append("<td>"+ data.alarmPushConfigs[i].targetName +"</td>");
                $trTemp.append("<td>"+ data.alarmPushConfigs[i].targetPhoneNumber +"</td>");
                $trTemp.append("<td>"+ data.alarmPushConfigs[i+1].targetName +"</td>");
                $trTemp.append("<td>"+ data.alarmPushConfigs[i+1].targetPhoneNumber +"</td>");
                $trTemp.appendTo("#contacts");
            }
            var $trTemp = $("<tr></tr>");
            $trTemp.append("<td>"+ data.alarmPushConfigs[length-1].targetName +"</td>");
            $trTemp.append("<td>"+ data.alarmPushConfigs[length-1].targetPhoneNumber +"</td>");
            $trTemp.append("<td>"+ '' +"</td>");
            $trTemp.append("<td>"+ '' +"</td>");
            $trTemp.appendTo("#contacts");

            for(var j=0;j<6-length-1;j=j+2){
                var $trTemp = $("<tr></tr>");
                $trTemp.append("<td>"+ "" +"</td>");
                $trTemp.append("<td>"+ "" +"</td>");
                $trTemp.append("<td>"+ '' +"</td>");
                $trTemp.append("<td>"+ '' +"</td>");
                $trTemp.appendTo("#contacts");
            }
        }
    };
    //监控点表格的保存处理
    service.formateMonitoringPointSave = function () {
        var tableName = "#monitoringPoint "
        Global_Data.baseInfo.address = $(tableName+"tbody tr:eq(1) td:eq(1)").html();
        Global_Data.baseInfo.longitude = $(tableName+"tbody tr:eq(1) td:eq(2)").html();
        Global_Data.baseInfo.latitude = $(tableName+"tbody tr:eq(1) td:eq(3)").html();
        Global_Data.baseInfo.markInstallationWay = $(tableName+"tbody tr:eq(1) td:eq(4)").html();
        var enableStatus = $(tableName+"tbody tr:eq(1) td:eq(5)").html();
        if(enableStatus === '启用'){
            Global_Data.baseInfo.enableStatus = 1;
        }else{
            Global_Data.baseInfo.enableStatus = 0;
        }
    };
    //污染物信息表格的保存处理
    service.formatContaminantsSave = function () {
        var tableName = "#contaminants";
        if(parent.monitorPointId === '1'){
            for(var i=1;i<=3;i++){
                Global_Data.thresholdConfigs[i-1].normalValueLowerLimit = $(tableName+" tbody tr:eq("+i+") td:eq(1)").html();
                Global_Data.thresholdConfigs[i-1].normalValueUpperLimit = $(tableName+" tbody tr:eq("+i+") td:eq(2)").html();
                Global_Data.thresholdConfigs[i-1].alarmUpperLimit = $(tableName+" tbody tr:eq("+i+") td:eq(3)").html();
                Global_Data.thresholdConfigs[i-1].alarmLowerLimit = $(tableName+" tbody tr:eq("+i+") td:eq(4)").html();
                var status = $(tableName+" tbody tr:eq("+i+") td:eq(5)").html();
                if(status === '启用'){
                    Global_Data.thresholdConfigs[i-1].status = 1
                }else{
                    Global_Data.thresholdConfigs[i-1].status = 0
                }
            }
        }else{
            for(var i=1;i<=2;i++){
                Global_Data.thresholdConfigs[i-1].normalValueLowerLimit = $(tableName+" tbody tr:eq("+i+") td:eq(1)").html();
                Global_Data.thresholdConfigs[i-1].normalValueUpperLimit = $(tableName+" tbody tr:eq("+i+") td:eq(2)").html();
                Global_Data.thresholdConfigs[i-1].alarmUpperLimit = $(tableName+" tbody tr:eq("+i+") td:eq(3)").html();
                Global_Data.thresholdConfigs[i-1].alarmLowerLimit = $(tableName+" tbody tr:eq("+i+") td:eq(4)").html();
                var status = $(tableName+" tbody tr:eq("+i+") td:eq(5)").html();
                if(status === '启用'){
                    Global_Data.thresholdConfigs[i-1].status = 1
                }else{
                    Global_Data.thresholdConfigs[i-1].status = 0
                }
            }
        }

    };
    //联系方式表格的保存处理
    service.formatContactSave = function () {
        var nameArray = [];//姓名数组
        var phoneArray = [];//电话数组
        $('#contacts tbody tr').find('td').each(function(index) {
            if(index%2 === 0 ){
                nameArray.push($(this).html());
            }else {
                phoneArray.push($(this).html());
            }
        });
        console.log(nameArray,phoneArray);
        Global_Data.alarmPushConfigs.map(function (item,index) {
            item.targetName= nameArray[index];
            item.targetPhoneNumber = phoneArray[index];
        });
        var globalDataLength = Global_Data.alarmPushConfigs.length;
        var allDataLength = nameArray.length;
        if(allDataLength > globalDataLength){
            for(var i=globalDataLength;i<(allDataLength);i++){
                if(nameArray[i].length>=1){
                    var t= {};
                    t.targetName=nameArray[i];
                    t.targetPhoneNumber = phoneArray[i];
                    t.monitorPointId= parent.monitorPointId;
                    Global_Data.alarmPushConfigs.push(t);
                }
            }
        }
    };
    return service;
}

var essentialService = EssentialService();

function init() {
    //编辑
    $("#edit").on('click', function () {
        $("#save").attr('hidden', false);
        $("#cancel").attr('hidden', false);
        contaminants = $('#contaminants').html();
        monitoringPoint = $("#monitoringPoint").html();
        contacts = $("#contacts").html();
        //监控点表格
        for(var j=1;j<=1;j++){
            var ttr = $('#monitoringPoint tr').eq(j);
            ttr.find("td").each(function (item) {
                var temp = [];
                //首项不编辑
                if(item !=0 ){
                    //除了第五项都用input
                    if(item != 5 ){
                        var tdText = $(this).html();
                        $(this).html("");
                        var inputObj = $("<input type='text'>");
                        inputObj.appendTo($(this));
                        if(item === 2 || item === 3){
                            inputObj.css("width","65px");
                        }else if(item === 4){
                            inputObj.css("width","45px");
                        }else{
                            inputObj.css("width","35px");
                        }
                        inputObj.val(tdText);
                    }
                    //使用select下拉框
                    else{
                        var tdText = $(this).html();
                        $(this).html("");
                        var inputObj;
                        if(tdText === '启用'){
                            inputObj = $('<select style="background-color: #0C0C0C;"><option selected="selected">启用</option><option>禁用</option></select>');
                        }else{
                            inputObj = $('<select style="background-color: #0C0C0C;"><option >启用</option><option selected="selected">禁用</option></select>');
                        }
                        inputObj.appendTo($(this));
                        inputObj.css("width","50px");
                        // inputObj.val(tdText);
                    }

                }
            });
        }
        //污染物表格
        for(var i=1;i<=3;i++){
            var ttr = $('#contaminants tr').eq(i);
            ttr.find("td").each(function (item) {
                var temp = [];
                //首项不编辑
                if(item !=0 ){
                    //除了第五项都用input
                    if(item != 5 ){
                        var tdText = $(this).html();
                        $(this).html("");
                        var inputObj = $("<input type='text'>");
                        inputObj.appendTo($(this));
                        inputObj.css("width","25px");
                        inputObj.val(tdText);
                    }
                    //使用select下拉框
                    else{
                        var tdText = $(this).html();
                        $(this).html("");
                        var inputObj;
                        if(tdText === '启用'){
                            inputObj = $('<select style="background-color: #0C0C0C;"><option selected="selected">启用</option><option>禁用</option></select>');
                        }else{
                            inputObj = $('<select style="background-color: #0C0C0C;"><option >启用</option><option selected="selected">禁用</option></select>');
                        }
                        inputObj.appendTo($(this));
                        inputObj.css("width","50px");
                    }

                }
            });
        }
        //联系人表格
        for(var k=0;k<=2;k++){
            var ttr = $('#contacts tr').eq(k);
            ttr.find("td").each(function (item) {
                var temp = [];
                var tdText = $(this).html().replace(/&nbsp;/ig, "");
                $(this).html("");
                var inputObj = $("<input type='text'>");
                inputObj.appendTo($(this));
                if(item === 1 || item === 3){
                    inputObj.css("width","80px");
                }else{
                    inputObj.css("width","40px");
                }
                inputObj.val(tdText);
            });
        }
        $(this).hide();
    });
    //保存
    $("#save").on('click', function () {
        $("#save").attr('hidden', true);
        $("#cancel").attr('hidden', true);
        // 数据抽离
        //监控点表格
        for(var j=1;j<=1;j++){
            var ttr = $('#monitoringPoint tr').eq(j);
            ttr.find("td").each(function (item) {
                var temp = [];
                //首项不编辑
                if(item !=0 ){
                    //除了第五项都用input
                    if(item != 5 ){
                        var tdText = $(this).children().val();
                        //console.log(tdText);
                        $(this).html(tdText);
                    }
                    //使用select下拉框
                    else{
                        var tdText = $(this).children().val();
                        $(this).html(tdText);
                    }
                }
            });
        }
        // //污染物表格
        for(var i=1;i<=3;i++){
            var ttr = $('#contaminants tr').eq(i);
            ttr.find("td").each(function (item) {
                var temp = [];
                //首项不编辑
                if(item !=0 ){
                    //除了第五项都用input
                    if(item != 5 ){
                        var tdText = $(this).children().val();
                        $(this).html(tdText);
                    }
                    //使用select下拉框
                    else{
                        var tdText = $(this).children().val();
                        $(this).html(tdText);
                    }

                }
            });
        }
        // //联系人表格
        for(var k=0;k<=2;k++){
            var ttr = $('#contacts tr').eq(k);
            ttr.find("td").each(function (item) {
                var tdText = $(this).children().val();
                $(this).html(tdText);
            });
        }
        essentialService.formatContaminantsSave();
        essentialService.formateMonitoringPointSave();
        essentialService.formatContactSave();
        essentialService.updateMonitorPointBaseInfoById(Global_Data);
        $("#edit").show();
    });
    //取消
    $("#cancel").on('click', function () {
        $("#save").attr('hidden', true);
        $("#cancel").attr('hidden', true);
        $('#contaminants').html(contaminants);
        $('#monitoringPoint').html(monitoringPoint);
        $('#contacts').html(contacts);
        $("#edit").show();
    });
    //数据加载
    essentialService.getMonitorPointBaseInfoById(parent.monitorPointId);
}
$(function () {
    init();
});
